#!/usr/bin/env python
import imp
import os
import unittest

ubiqfs = imp.new_module('ubiqfs')
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 
                    '../bin/ubiqfs') 

exec file(path) in ubiqfs.__dict__


class UbiqisPathInfoTest(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self.path = None
        self.utype = None
        self.expected = None
        unittest.TestCase.__init__(self, *args, **kwargs)

    def set_info(self, path, utype, expected):
        self.path = path
        self.utype = utype
        self.expected = expected

    def shortDescription(self):
        return 'UbiqisPathInfoTest(path=%r)' % (self.path,)

    def runTest(self):
        if self.path is None:
            # By itself, this test does nothing; use the test suite
            return
        info = ubiqfs.UbiqisPathInfo(self.path)
        self.assertEquals(info.utype_name, 'UTYPE_' + self.utype.upper())
        for key, exp_value in self.expected.iteritems():
            value = getattr(info, key)
            if key == 'link_target':
                exp_value = '/mnt/point' + exp_value
                temp = '/mnt/point' + self.path
                pre_pieces = [p for p in temp.split('/') if p][:-1]
                post_pieces = [p for p in value.split('/') if p]
                while pre_pieces and post_pieces and post_pieces[0] == '..':
                    pre_pieces.pop()
                    post_pieces.pop(0)
                value = '/' + '/'.join(pre_pieces + post_pieces)
            msg = '%r != %r (key=%r)' % (value, exp_value, key)
            self.assertEquals(value, exp_value, msg)


def test_ubiqis_pathinfo():
    paths = {
        '/': ('root', {}),
        '/__init__': ('none', {}),
        '/edu': ('parent_dir', {}),
        '/edu/__init__.py': ('init_py', {}),
        '/edu/lsu/cct/ubiq2': ('parent_dir', {}),
        '/edu/lsu/cct/ubiq2/_tmkemp2': ('parent_dir', {}),
        '/edu/lsu/cct/ubiq2/_tmkemp2/__init__.py': ('init_py', {}),
        '/edu/lsu/cct/ubiq2/_tmkemp2/libpng': ('link', {
            'link_target': '/package-edu.lsu.cct.ubiq2._tmkemp2.libpng/data',
        }),
        '/libedu-lsu-cct-ubiq2-_tmkemp2-libpng-foo.so': ('link', {
            'link_target': '/package-edu.lsu.cct.ubiq2._tmkemp2.libpng/'
            'libedu-lsu-cct-ubiq2-_tmkemp2-libpng-foo.so',
        }),
        '/package-edu.lsu.cct.ubiq2._tmkemp2.libpng': ('parent_dir', {}),
        '/package-edu.lsu.cct.ubiq2._tmkemp2.libpng/data': ('ubiq_path', {
            'pkgname': 'edu.lsu.cct.ubiq2._tmkemp2.libpng',
            'post_pkg_path': '',
        }),
        '/package-edu.lsu.cct.ubiq2._tmkemp2.libpng/data/foo': ('ubiq_path', {
            'pkgname': 'edu.lsu.cct.ubiq2._tmkemp2.libpng',
            'post_pkg_path': 'foo',
        }),
        ('/package-edu.lsu.cct.ubiq2._tmkemp2.libpng/'
         'libedu-lsu-cct-ubiq2-_tmkemp2-libpng-foo.so'): ('ubiq_lib_path', {
            'pkgname': 'edu.lsu.cct.ubiq2._tmkemp2.libpng',
        }),
        '/x86_64': ('none', {}),
        '/config': ('none', {}),
        '/config.py': ('config', {}),
        '/foo.py': ('none', {}),
    }

    suite = unittest.TestSuite()
    for path in sorted(paths):
        utype, data = paths[path]
        test = UbiqisPathInfoTest()
        test.set_info(path, utype, data)
        suite.addTest(test)

    return suite

if __name__ == '__main__':
    unittest.TextTestRunner().run(test_ubiqis_pathinfo())
